<?php

class Waremodel extends CI_Model{
	
    var $conn;
	
    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    	$user = "automatsystem";
	$pass = "freemachineskillhumans";
	error_reporting(0);
	$this->conn = mysql_connect("localhost:3306",$user,$pass);
	if(!$this->conn){
		$this->conn = mysql_connect("localhost:3307",$user,$pass);	
	}
	if(!$this->conn){
		$this->conn = mysql_connect("localhost:3308",$user,$pass);	
	}
	if(!$this->conn){
		$this->conn = mysql_connect("localhost:3309",$user,$pass);
	}
	error_reporting(E_ALL);
	if(!$this->conn){
		echo '*ERROR* Usermodel: Can\'t connect to MySQL server.';
	}
	else{
		mysql_select_db("automatsystem");
	}
    }

    function testConnection(){
	if($this->conn){return true;}
    	return false;
    }
	
    /*
    	Returns columns : WareId, Price, Amount, AutomatId, ProductCode, CanSell, SellFrom, SellTo...
    	Example:
    		$query = $this->Waremodel->getAllWares();
    		echo $query[0]['WareId'];
    */
    function getAllWares(){
            $queryRes = mysql_query("SELECT * FROM Ware;",$this->conn);
            $retArr = array();
            while($row = mysql_fetch_assoc($queryRes)){
                    $retArr[] = $row;
            }
            return $retArr;
    }
    
    function getWare($wareId){
        $queryRes = mysql_query("SELECT * FROM Ware WHERE WareId=".$wareId,$this->conn);
        if(!$queryRes){ echo mysql_error();}
        return mysql_fetch_array($queryRes);
        
    }
	
    //double(10,2), int, int, int, bool, TIME, TIME(TIME -�� ����� ������ ������� : "hh:mm:ss")
    function addWare($price, $amount, $automatId, $productCode, $canSell, $sellFrom, $sellTo){
            $res = mysql_query("INSERT INTO Ware(Price, Amount, AutomatId, ProductCode, CanSell, SellFrom, SellTo)
            VALUES('".$price."','".$amount."','".$automatId."','".$productCode."',".($canSell?'(1)':'(0)').",'".$sellFrom."','".$sellTo."');",$this->conn);
            if(!$res){echo '<p>Error adding ware. </p>';}
            return $res;
    }
	
	//int
    function deleteWare($wareId){
	$res = mysql_query("DELETE FROM Ware WHERE WareId=".$wareId.";");
	if(!$res){echo '<p>Error deleting ware. </p>';}
	return $res;
    }
    /*
            ���� ��� � ����� � ������� WareId. ���� �� ����� ����� ���� ��� - ������� ������ ��� null.
            CanSell �������� ������� ��������, ������� (�� � �������) � PHP null i false - �� ���� ����.
            ���������: int, double(10,2), int, int, int, TIME, TIME. (TIME -�� ����� ������ ������� : "hh:mm:ss")
    */
    function updateWare($wareId, $price, $amount, $automatId, $productCode, $sellFrom, $sellTo){
            $setString = '';
            $valuePresent = false;
            if($price) {
                    $setString .= "Price=".$price;
                    $valuePresent = true;
            }
            if ($amount) {
                    if($valuePresent) $setString .= ',';
                    $setString .= "Amount=".$amount;
                    $valuePresent = true;
            }
			if ($amount == 0) {
                    if($valuePresent) $setString .= ',';
                    $setString .= "Amount=".$amount;
                    $valuePresent = true;
            }
            if($automatId){
                    if($valuePresent) $setString .= ',';
                    $setString .= "AutomatId=".$automatId;
                    $valuePresent = true;
            }
            if($productCode) {
                    if($valuePresent) $setString .= ',';
                    $setString .= "ProductCode=".$productCode;
                    $valuePresent = true;
            }
            if($sellFrom) {
                    if($valuePresent) $setString .= ',';
                    $setString .= "SellFrom='".$sellFrom."'";
                    $valuePresent = true;
            }
            if($sellTo) {
                    if($valuePresent) $setString .= ',';
                    $setString .= "SellTo='".$sellTo."'";
                    $valuePresent = true;
            }
            $res = mysql_query("UPDATE Ware SET ".$setString." WHERE WareId=".$wareId.";");
            echo mysql_error();
            if(!$res){echo '<p>Error updating ware. WareId='.$wareId.'. </p>';}
            return $res;
    }
	
    //int,bool
    function setCanSell($wareId,$canSell){
            $res = mysql_query("UPDATE Ware SET CanSell=".($canSell?'(1)':'(0)')." WHERE WareId=".$wareId.";");
            if(!$res){echo '<p>Error setting CanSell. WareId='.$wareId.'. </p>'; echo mysql_error();}
            return $res;
    }

    function productName($productCode)
    {
        $res = mysql_query('SELECT Name FROM Product WHERE ProductCode='.$productCode,$this->conn);
        $data = mysql_fetch_array($res);
        return $data['Name'];
    }
	
    function short_info($productCode)
    {
        $res = mysql_query('SELECT ShortInfo FROM Product WHERE ProductCode='.$productCode,$this->conn);
        $data = mysql_fetch_array($res);
        return $data['ShortInfo'];
    }
	
    function getAutomatWares($id){
        $res = mysql_query('SELECT * FROM Ware WHERE AutomatId='.$id,$this->conn);
        $retArr = array();
	while($row = mysql_fetch_assoc($res)){
		$retArr[] = $row;
	}
	return $retArr;
    }
    
    function getAutomatWaresExt($id){
        $res = mysql_query('SELECT * FROM Ware INNER JOIN Product ON Ware.ProductCode = Product.ProductCode WHERE AutomatId='.$id,$this->conn);
        if(!$res){echo 'Error: '.mysql_error();}
        $retArr = array();
	while($row = mysql_fetch_array($res)){
		$retArr[] = $row;
	}
	return $retArr;
    }
	
    
	
	
}